1736B - Playing with GCD - CodeForces Solution


math number theory

Please click on ads to support us..

Python Code:

import math
for i in range (int(input())):
    x=int(input())
    a=list(map(int,input().split()))
    b=[0]*(x+1)
    b[0]=a[0]
    b[1]=a[0]
    for i in range(1,x):
        
        b[i+1]=a[i]
        g=math.gcd(b[i],a[i])
        b[i]=b[i]*a[i]//g
        
    check=0
    for i in range(x):
        if math.gcd(b[i],b[i+1])!=a[i]:
            check=1
            break
    if check:print('NO')
    else:print('YES')

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define int  long
#define lll __int128
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define endl '\n'
const long long INF = 1ll << 32;
const long double PI = acos(-1);
const int N = 1000001, Mod = 1000000007 , inf = 1e9 , bits = 27;
const int NN = 1e9 , OO = 0x3F3F3F3F;
#define NeedForSpeed ios_base::sync_with_stdio(false) , cin.tie(0),cout.tie(0);
int lcm(int a,int b) {
    int g = __gcd(a, b);
    return (a * b / g);
}
void solve () {
    int n;
    cin >> n;
    vector<int> a(n + 2 , 1), b(n + 2 , 1);
    for (int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    for (int i = 1;i <= n + 1;i++){
            b[i] = lcm (a[i - 1] , a[i]);
    }
    bool ok = true;
    
    for (int i = 1; i <= n; i++){
        if (__gcd(b[i] , b[i + 1]) != a[i])
            ok = false;
    }
    cout << (ok ? "YES" : "NO") << endl;
}
int32_t main() {
    NeedForSpeed
    int test_cases = 1;
    cin >> test_cases;
    while (test_cases--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One